package com.fengzai;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @PACKAGE_NAME: com.fengzai
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/11/9
 **/
public class Dto {
    private int val;

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public Dto(int val) {
        this.val = val;
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        Dto dto = Dto.class.getConstructor().newInstance();
        Constructor<?> constructor = (Dto.class.getConstructors())[0];
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        Object obj;
        if (parameterTypes.length > 0) {
            Object[] o = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                String typeName = parameterTypes[i].getTypeName();
                if ("int".equals(typeName)
                        || "short".equals(typeName)
                        || "int".equals(typeName)) {
                    o[i] = 0;
                } else if ("long".equals(typeName)) {
                    o[i] = 0l;
                } else if ("float".equals(typeName)) {
                    o[i] = 0f;
                } else if ("double".equals(typeName)) {
                    o[i] = 0d;
                } else if ("char".equals(typeName)) {
                    o[i] = '\u0000';
                } else if ("boolean".equals(typeName)) {
                    o[i] = false;
                } else {
                    o[i] = null;
                }
            }
            obj = constructor.newInstance(o);
        } else {
            obj = constructor.newInstance();
        }
        Field[] fields = obj.getClass().getFields();

        System.out.println(obj);
    }
}
